﻿@model Bricks.Site.Models.BridgeStatisticsView
@{
    ViewBag.Title = "桥梁明细";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<script type="text/javascript">
    $(function () {

        var bridgeID = $("#hID").val();
        BridgeStatisticsDetail.InitBridgeDetailGrid(bridgeID);

        //统计周期格式化
        $("#StatisticalCycle").datebox({
            required: "true",
            missingMessage: "必填项，选择后将生成对应年月的统计周期",
            formatter: function (date) {
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                //var d = date.getDate();
                return y + "年" + (m < 10 ? ("0" + m) : m) + "月";
            },
            //每次选择统计周期后，自动取上月的明细，以供本月修改
            onSelect: function (date) {
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var projectID = $("#hProjectID").val();
                var contractID = $("#hContractID").val();
                //先判断该统计周期是否存在，存在则进行提示
                $.post("/BridgeStatistics/IsExistStatisticalCycle?year=" + y + "&month=" + m + "&projectID=" + projectID + "&contractID=" + contractID, function (isExist) {
                    //$.EasyuiSlide(isExist);
                    if (isExist == "true") {
                        $.EasyuiMsg("该统计周期已存在，请重新选择");
                        BridgeStatisticsDetail.InitBridgeDetailGrid($("#hID").val());
                        return;
                    }
                    else {
                        //取上月的明细数据补充到明细表中
                        $.post("/BridgeStatistics/GetPreStatisticalCycleBridgeID?year=" + y + "&month=" + m + "&projectID=" + projectID + "&contractID=" + contractID, function (preBridgeID) {
                            //$.EasyuiSlide(preBridgeID);
                            if (preBridgeID != "") {
                                $.EasyuiSlide("已自动拷贝上个统计周期的明细数据");
                                BridgeStatisticsDetail.InitBridgeDetailGrid(preBridgeID, $("#hID").val());
                            }
                            else {
                                BridgeStatisticsDetail.InitBridgeDetailGrid($("#hID").val());
                            }
                        })
                    }
                })
            }
        });
    });

    var BridgeStatisticsDetail = {
        //初始化桥梁明细列表
        InitBridgeDetailGrid: function (bridgeID, newBridgeID) {
            $('#bridgeDetailEditGrid').datagrid({
                url: '/BridgeStatistics/GetBridgeDetailList?bridgeID=' + bridgeID + "&newBridgeID=" + newBridgeID,
                nowrap: true,
                autoRowHeight: false,
                striped: true,
                collapsible: true,
                rownumbers: true,
                remoteSort: false,
                idField: 'ID',
                checkOnSelect: true,
                selectOnCheck: true,
                singleSelect: true,
                columns: [[
                    { title: 'ID', field: 'ID', hidden: true },
                    { title: 'BridgeStatisticsID', field: 'BridgeStatisticsID', hidden: true },
                    {
                        title: '操作', field: 'DeleteAction', width: 45, rowspan: 3, align: 'center',
                        formatter: function (value, row, index) {
                            return '<a href="#" onclick="BridgeStatisticsDetail.Delete(this)">删除</a> ';
                        }
                    },
                    { title: '桥梁名称', field: 'BridgeName', width: 120, rowspan: 3, align: 'center', editor: 'text' },
                    { title: '中心桩号', field: 'CenterPileNumber', width: 100, rowspan: 3, align: 'center', editor: 'text' },
                    { title: '结构形式', field: '', width: 290, colspan: 2, align: 'center' },
                    { title: '孔数*孔径(孔*m)', field: 'BoreNumAndDiameter', width: 100, rowspan: 3, align: 'center', editor: 'text' },
                    { title: '桥长(m)', field: 'BridgeLength', width: 90, rowspan: 3, align: 'center', editor: 'text' },
                    { title: '开工时间', field: 'StartTime', width: 115, rowspan: 3, align: 'center', editor: 'datebox' },
                    { title: '计划完成时间', field: 'PlanFinishTime', width: 115, rowspan: 3, align: 'center', editor: 'datebox' },
                    { title: '形象进度', field: '', width: 900, colspan: 11, align: 'center' }
                ],
                    [
                         { title: '下部结构', field: 'BottomStruct', align: 'center', rowspan: 3, width: 200, editor: 'text' },
                         { title: '上部结构', field: 'TopStruct', align: 'center', rowspan: 3, width: 90, editor: 'text' },
                         { title: '基础（m3）', field: '', colspan: 2, align: 'center', width: 160 },
                         { title: '下部构造（m3）', field: '', colspan: 2, align: 'center', width: 160 },
                         { title: '预制安装现浇(片)', field: '', colspan: 2, align: 'center', width: 160 },
                         { title: '上部构造(m3)', field: '', colspan: 2, align: 'center', width: 160 },
                         { title: '桥面及附属工程(m2)', field: '', colspan: 2, align: 'center', width: 160 },
                         { title: '总完成进度(%)', field: 'TotalFinishedProgress', rowspan: 2, align: 'center', width: 100, readonly: true }
                    ],
                    [
                         { title: '设计', field: 'BasicDesign', align: 'center', width: 80, editor: 'text' },
                         { title: '实际', field: 'BasicReality', align: 'center', width: 80, editor: 'text' },
                         { title: '设计', field: 'BottomBuildDesign', align: 'center', width: 80, editor: 'text' },
                         { title: '实际', field: 'BottomBuildReality', align: 'center', width: 80, editor: 'text' },
                         { title: '设计', field: 'PrecutInstallDesign', align: 'center', width: 80, editor: 'text' },
                         { title: '实际', field: 'PrecutInstallReality', align: 'center', width: 80, editor: 'text' },
                         { title: '设计', field: 'TopBuildDesign', align: 'center', width: 80, editor: 'text' },
                         { title: '实际', field: 'TopBuildReality', align: 'center', width: 80, editor: 'text' },
                         { title: '设计', field: 'BridgeSurfaceDesign', align: 'center', width: 80, editor: 'text' },
                         { title: '实际', field: 'BridgeSurfaceReality', align: 'center', width: 80, editor: 'text' }
                    ]],
                toolbar: '#bridgeDetailEditToolBar',
                onLoadSuccess: function (data) {

                    if (data.rows.length > 0) {
                        BridgeStatisticsDetail.EditAllRow();
                    }
                }
            });
        },

        //新增
        Add: function () {
            var statisticalCycle = $('#StatisticalCycle').datebox('getValue');
            if (statisticalCycle == "") {
                $.EasyuiMsg("统计周期不能为空");
            }
            else {
                $("#bridgeDetailEditGrid").datagrid("appendRow", { BridgeStatisticsID: $("#hID").val() });
                BridgeStatisticsDetail.EditRow($('#bridgeDetailEditGrid').datagrid('getRows').length - 1);  //使新增行也处于编辑状态
            }

            //$.EasyuiMsg(statisticalCycle);
        },

        //编辑行
        EditRow: function (currentIndex) {
            $('#bridgeDetailEditGrid').datagrid('beginEdit', currentIndex);
        },

        //结束编辑
        EndEdit: function () {
            var rows = $('#bridgeDetailEditGrid').datagrid('getRows');
            for (var i = 0; i < rows.length; i++) {
                $('#bridgeDetailEditGrid').datagrid('endEdit', i);
            }
        },

        //编辑所有行
        EditAllRow: function () {
            var rows = $('#bridgeDetailEditGrid').datagrid('getRows');
            for (var i = 0; i < rows.length; i++) {
                $('#bridgeDetailEditGrid').datagrid('beginEdit', i);
            }
        },

        //删除
        Delete: function (target) {
            //$.EasyuiMsg("删除");

            var tr = $(target).closest('tr.datagrid-row');
            var index = parseInt(tr.attr('datagrid-row-index'));
            $('#bridgeDetailEditGrid').datagrid('deleteRow', index);
            var list = $('#bridgeDetailEditGrid').datagrid('getData').rows;
            for (var i = 0; i < list.length; i++) {
                EditRow(i);
            }
        },

        //保存
        Save: function () {
            //$.EasyuiMsg("保存");
            //
            //BridgeStatisticsDetail.EndEdit();  //结束编辑

            //
            //var data = $('#bridgeDetailEditGrid').datagrid('getRows');  //取得所有数据
            //var dataJson = JSON.stringify(data);
            ////var effectRow = new Object();
            ////effectRow["Data"] = dataJson;

            //$("#hBridgeDetailListJson").val(dataJson);

            //$.post("/BridgeStatistics/Save", function (res) {
            //    $.EasyuiMsg("post保存");
            //})


            //new
            $('#bridgeDetailEditGrid').datagrid('acceptChanges');
            var data = $('#bridgeDetailEditGrid').datagrid('getData').rows;
            var dataJson = JSON.stringify(data);
            $('#hBridgeDetailListJson').val(dataJson);
            $('#frmBridgeStatistics').form('submit', {
                url: '/BridgeStatistics/Save',
                onSubmit: function () {

                },
                success: function (result) {
                    result = JSON.parse(result);
                    if (result.ResultType == 0) {  //ResultType是在OperationResultType定义的，0表示Success成功
                        $('#btnCirculation').linkbutton('enable');
                        $('#btnCirculated').linkbutton('enable')
                        $.EasyuiSlide(result.Message);  //弹出右下角提示
                    }
                    else {
                        $.EasyuiMsg(result.Message);  //弹出提示窗体
                    }
                    //if (data != "保存成功！") {
                    //    $.EasyuiSlide(data);
                    //}
                    //else {
                    //    $('#btnCirculation').linkbutton('enable');
                    //    $('#btnCirculated').linkbutton('enable');
                    //    $.EasyuiSlide(data);
                    //}
                    //if (yearPlanDetailList.length > 1) {
                    //    mergeCellsByField("yearPlanDetailsDataGrid", "ParentStatisticsItemName,StatisticsItemName");
                    //}
                    //if (yearPlanDetailList.length > 0) {
                    //    mergeColCells("yearPlanDetailsDataGrid");
                    //}
                    BridgeStatisticsDetail.EditAllRow();
                }
            });
        },

        //关闭当前窗体
        CloseSelf: function () {
            parent.cancelPopWindow($("#hHandleState").val());
        },

        //预览流程图
        PreviewProcessGraph: function (title, index) {
            if (index == 1) {
                var flowCode = $("#hFlowCode").val();
                var id = $("#hID").val();
                $.post("/YearPlan/GenerateGraph?flowCode=" + flowCode + "&id=" + id, function (res) {
                    var container = document.getElementById('graphContainer');
                    if (container.hasChildNodes()) {
                        $("#graphContainer").empty()
                    }
                    PreviewGraph(container, res);
                })
            }
        },

        //发送
        Circulation: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            var handleState = $("#hHandleState").val();
            var nodeCode = $("#hAssociatedNodeCode").val();
            var processUserDepartment = $("#hLastUpdateUserDepartment").val();
            var processUser = $("#hLastUpdateUser").val();

            showMyWindow("#bridgeStatisticsWindow", "流程审批", "/Flow/CirculationFlow?flowCode=" + flowCode + "&recordID=" + id + "&handleState=" + handleState + "&currentFlowNodeCode=" + nodeCode + "&processUserDepartment=" + processUserDepartment + "&processUser=" + processUser, 500, 250, true, false);
        },

        //撤回
        Withdraw: function () {
            var withdrawNodeCode = $("#hProcessdOrCirculatedNodeCode").val();
            $('#frmBridgeStatistics').form('submit', {
                url: '/BridgeStatistics/Withdraw?withdrawNodeCode=' + withdrawNodeCode,
                onSubmit: function () {

                },
                success: function (data) {
                    window.parent.CloseWindow('撤回成功！');
                },
                fail: function (data) {
                    $.EasyuiSlide(data);
                }
            });
        },

        //回退
        Back: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            var nodeCode = $("#hAssociatedNodeCode").val();
            showMyWindow("#bridgeStatisticsWindow", "流程回退", "/Flow/BackFlow?flowCode=" + flowCode + "&recordID=" + id + "&flowNodeCode=" + nodeCode, 550, 400, true, false);
        },

        //终止
        Cancel: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            $.post("/BridgeStatistics/Cancel?flowCode=" + flowCode + "&id=" + id, function (res) {
                if (res == "Success") {
                    window.parent.CloseWindow('终止成功！');
                }
                else {
                    $.EasyuiSlide(res);
                }
            });
        },

        //传阅
        Circulated: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            var nodeCode = $("#hProcessdOrCirculatedNodeCode").val();
            var department = $("#hProcessdOrCirculatedUserDepartment").val();
            showMyWindow("#bridgeStatisticsWindow", "流程传阅", "/Flow/CirculatedFlow?flowCode=" + flowCode + "&recordID=" + id + "&nodeCode=" + nodeCode + "&department=" + department, 400, 400, true, false);
        },

        //已阅
        Receipt: function () {
            var flowCode = $("#hFlowCode").val();
            var messageID = $("#hSourceID").val();
            showMyWindow("#bridgeStatisticsWindow", "流程已阅", "/Flow/ReceiptFlow?flowCode=" + flowCode + "&messageID=" + messageID, 500, 250, true, false);
        },

        //催办
        Reminders: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            $.post("/BridgeStatistics/Reminders?flowCode=" + flowCode + "&id=" + id, function (res) {
                if (res == "Success") {
                    window.parent.CloseWindow('催办成功！');
                }
                else {
                    $.EasyuiSlide(res);
                }
            });
        }
    }

    function CloseWindow(closeType, data) {
        colseMyWindow("#bridgeStatisticsWindow");
        if (closeType != 0) {
            window.parent.CloseWindow(data);
        }
    }
</script>

<div class="easyui-panel" fit="true">
    <div id="divBridge" class="easyui-tabs" fit="true" data-options="onSelect:BridgeStatisticsDetail.PreviewProcessGraph">
        <div title="桥梁明细" data-options="iconCls:'icon-none'" style="padding: 2px;">
            <div style="min-width: 1200px; overflow: auto">
                <div style="border: 1px solid #ddd; margin-bottom: 3px;">
                    <div id="myToolBar">
                        @Html.Raw(ViewBag.ToolBarInfo)
                        @*<a href='javascript:void(0)' class='easyui-linkbutton' onclick='BridgeStatisticsDetail.Save()' data-options='plain:true,iconCls:"icon-bell"'>保存</a>
                            <a href="javascript:void(0)" id="btnCancel" class="easyui-linkbutton" onclick="BridgeStatisticsDetail.Circulation();" data-options="plain:true,iconCls:'icon-ok'">发送</a>
                            <a href="javascript:void(0)" id="btnCancel" class="easyui-linkbutton" onclick="BridgeStatisticsDetail.Withdraw();" data-options="plain:true,iconCls:'icon-ok'">撤回</a>
                            <a href="javascript:void(0)" id="btnCancel" class="easyui-linkbutton" onclick="BridgeStatisticsDetail.Back();" data-options="plain:true,iconCls:'icon-ok'">回退</a>
                            <a href="javascript:void(0)" id="btnCancel" class="easyui-linkbutton" onclick="BridgeStatisticsDetail.Cancel();" data-options="plain:true,iconCls:'icon-ok'">终止</a>
                            <a href="javascript:void(0)" id="btnCancel" class="easyui-linkbutton" onclick="BridgeStatisticsDetail.Circulated();" data-options="plain:true,iconCls:'icon-ok'">传阅</a>
                            <a href="javascript:void(0)" id="btnCancel" class="easyui-linkbutton" onclick="BridgeStatisticsDetail.Receipt();" data-options="plain:true,iconCls:'icon-ok'">已阅</a>
                            <a href="javascript:void(0)" id="btnCancel" class="easyui-linkbutton" onclick="BridgeStatisticsDetail.Reminders();" data-options="plain:true,iconCls:'icon-ok'">催办</a>*@
                        <a href="javascript:void(0)" id="btnCancel" class="easyui-linkbutton" onclick="BridgeStatisticsDetail.CloseSelf();" data-options="plain:true,iconCls:'icon-ok'">关闭</a>
                    </div>
                </div>
                <div>
                    <div class="easyui-layout" style="min-height: 1200px;">
                        <div region="north" split="true" style="width: auto; height: 40px; padding: 1px;">
                            <form id="frmBridgeStatistics" method="post">
                                <input type="hidden" id="hID" name="ID" value="@Model.ID" />
                                <input type="hidden" id="hFlowCode" name="FlowCode" value="@Model.FlowCode" />
                                <input type="hidden" id="hProjectID" name="ProjectID" value="@Model.ProjectID" />
                                <input type="hidden" id="hContractID" name="ContractID" value="@Model.ContractID" />
                                <input type="hidden" id="hRequestDate" name="RequestDate" value="@Model.RequestDate" />
                                <input type="hidden" id="hRequestPerson" name="RequestPerson" value="@Model.RequestPerson" />
                                <input type="hidden" id="hHandleState" name="HandleState" value="@Model.HandleState" />
                                <input type="hidden" id="hAssociatedNodeCode" name="AssociatedNodeCode" value="@Model.AssociatedNodeCode" />
                                <input type="hidden" id="hLastUpdateUserDepartment" name="LastUpdateUserDepartment" value="@Model.LastUpdateUserDepartment" />
                                <input type="hidden" id="hLastUpdateUser" name="LastUpdateUser" value="@Model.LastUpdateUser" />
                                <input type="hidden" id="hSaveState" name="SaveState" value="@Model.SaveState" />

                                <input type="hidden" id="hBridgeDetailListJson" name="BridgeDetailListJson" value="@Model.BridgeDetailListJson" />
                                <input type="hidden" id="hSourceType" name="SourceType" value="@Model.SourceType" />
                                <input type="hidden" id="hProcessType" name="ProcessType" value="@Model.ProcessType" />
                                <input type="hidden" id="hSourceID" name="SourceID" value="@Model.SourceID" />
                                <input type="hidden" id="hProcessdOrCirculatedNodeCode" name="ProcessdOrCirculatedNodeCode" value="@Model.ProcessdOrCirculatedNodeCode" />
                                <input type="hidden" id="hProcessdOrCirculatedUserDepartment" name="ProcessdOrCirculatedUserDepartment" value="@Model.ProcessdOrCirculatedUserDepartment" />
                                <table style="margin: 1px">
                                    <tr id="trRequest">
                                        <td style="width: 60px; text-align: right">统计周期：</td>
                                        <td>
                                            @*<input id="StatisticalCycle" name="StatisticalCycle" class="easyui-datebox" data-options="required:true" value="@Model.StatisticalCycle" style="width: 180px;" />*@
                                            <input id="StatisticalCycle" name="StatisticalCycle" type="text" value="@Model.StatisticalCycle" style="width: 180px;" />
                                        </td>
                                    </tr>
                                </table>
                            </form>
                        </div>
                        <div region="center" border="false">
                            <div class="easyui-layout" fit="true">
                                <div region="north" title="桥梁明细" style="width: auto; height: 400px; padding: 1px;">
                                    <table id="bridgeDetailEditGrid" fit="true"></table>
                                    <div id="bridgeDetailEditToolBar">
                                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='BridgeStatisticsDetail.Add()' data-options='plain:true,iconCls:"icon-application_add"'>新增</a>
                                    </div>
                                </div>
                                <div region="center" border="false">
                                    <div class="easyui-layout" fit="true">
                                        <div region="north" title="已办列表" style="width: auto; height: 400px; padding: 1px;">
                                            <table id="myDataGrid" fit="true"></table>
                                        </div>
                                        <div region="center" border="false">
                                            <div class="easyui-layout" fit="true">
                                                <div region="north" title="传阅列表" style="width: auto; height: 200px; padding: 1px;">
                                                    <table id="myCirculatedDataGrid" fit="true"></table>
                                                </div>
                                                <div region="center" title="已阅列表" style="width: auto; height: 200px; padding: 1px;">
                                                    <table id="myReceiptDataGrid" fit="true"></table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div title="流程图" data-options="iconCls:'icon-none'" style="padding: 10px;">
            <div style="width: 100%; height: 5%;">
                <img alt="" src="../../images/workflow.bmp" />
            </div>
            <div id="graphContainer" style="overflow-y: scroll; width: 100%; height: 95%; background: url('../../images/grid.gif'); cursor: default;">
            </div>
        </div>
    </div>
</div>
<div id="bridgeStatisticsWindow"></div>
